package com.rerun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rerun.entity.JobFeedback;
import com.rerun.entity.JobFeedbackDetail;
import com.rerun.enums.ResumeStateEnum;
import com.rerun.mapper.JobFeedbackDetailMapper;
import com.rerun.mapper.JobFeedbackMapper;
import com.rerun.service.IJobFeedbackService;
import com.rerun.service.IJobService;
import com.rerun.vo.park.InterviewVo;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;

/**
 * The type Job feedback service.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /10/10 16:00
 */
@Service
public class JobFeedbackServiceImpl extends ServiceImpl<JobFeedbackMapper, JobFeedback> implements IJobFeedbackService {
    @Resource
    private JobFeedbackMapper jobFeedbackMapper;
    @Resource
    private JobFeedbackDetailMapper detailMapper;


    @Override
    public List<JobFeedback> selectListNotInDetail() {
        return jobFeedbackMapper.selectListNotInDetail();
    }

    @Override
    public boolean updateStatus(InterviewVo interviewVo) {
        EntityWrapper<JobFeedback> wrapper = new EntityWrapper<>();
        wrapper.where("memberId={0} and jobId = {1}", interviewVo.getMemberId(), interviewVo.getJobId()).orderBy("createTime", false);
        List<JobFeedback> feedbacks = jobFeedbackMapper.selectList(wrapper);
        if (feedbacks != null && feedbacks.size() > 0) {
            JobFeedback feedback = feedbacks.get(0);
            if (!interviewVo.getStatus().equals(String.valueOf(feedback.getStatus()))) {
                feedback.setStatus(Long.parseLong(interviewVo.getStatus()));
                feedback.setUpdateTime(new Date());
                feedback.setRemark(interviewVo.getRemark());
                if (!"".equals(interviewVo.getInterviewTime()) && ResumeStateEnum.interview.getKey().equals(interviewVo.getStatus())) {
                    feedback.setInterviewTime(interviewVo.getInterviewTime());
                }
                jobFeedbackMapper.updateAllColumnById(feedback);

                addDetail(interviewVo, feedback);
            }
            return true;
        }else {
        	JobFeedback feedback = new JobFeedback();
        	feedback.setCreateTime(new Date());
        	feedback.setMemberId(interviewVo.getMemberId());
        	feedback.setUpdateTime(new Date());
        	feedback.setStatus(Long.parseLong(interviewVo.getStatus()));
            feedback.setUpdateTime(new Date());
            feedback.setRemark(interviewVo.getRemark());
            feedback.setJobId(interviewVo.getJobId());
            if (!"".equals(interviewVo.getInterviewTime()) && ResumeStateEnum.interview.getKey().equals(interviewVo.getStatus())) {
                feedback.setInterviewTime(interviewVo.getInterviewTime());
            }
            jobFeedbackMapper.insert(feedback);
            addDetail(interviewVo, feedback);
            return true;
        }
    }

	private void addDetail(InterviewVo interviewVo, JobFeedback feedback) {
		JobFeedbackDetail detail = new JobFeedbackDetail();
		detail.setCreateTime(new Date());
		detail.setJobFeedId(feedback.getId());
		detail.setRemark(interviewVo.getRemark());
		detail.setStatus(Long.parseLong(interviewVo.getStatus()));
		detail.setJobFeedId(feedback.getId());
		detailMapper.insert(detail);
	}

    @Override
    public Page<JobFeedback> selectByStatus(Page<JobFeedback> page, long status, String memberId) {
        List<JobFeedback> jobFeedbacks = jobFeedbackMapper.selectByStatus(page, status, memberId);
        page.setRecords(jobFeedbacks);
        return page;
    }
}
